package weeny.demo;

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.controls.ControlAdapter;
import prefuse.render.PolygonRenderer;
import prefuse.util.ui.UILib;
import prefuse.visual.VisualItem;

import stacked.graph.BelievableDataSource;
import stacked.graph.Layer;
import weeny.pre.display.BaseUIDisplay;
import weeny.pre.display.MoreVisualization;
import weeny.pre.renderer.StreamRenderer;
import weeny.pre.util.IntColor;
import weeny.pre.util.Marker;
import weeny.stackedGraph.pre.LinearColorPicker;
import weeny.stackedGraph.pre.StreamGraphLayout;

/**
 * Demo of stream graph layout.
 * 
 * @author weeny
 *
 */
public class StreamGraphDemo {
	public static void main(String[] args) {
		UILib.setPlatformLookAndFeel();
		BelievableDataSource bds = new BelievableDataSource(1230);
		//generated testing data: 100 stripes, 200 points each
		Layer[] ls = bds.make(10, 200);
		
		//setting up the interface and prefuse data
		MoreVisualization moreVis = new MoreVisualization();
		BaseUIDisplay display = new BaseUIDisplay(moreVis);
		
		Marker mark = moreVis.addMarker("Marker");
		mark.addDataNodeField("size", float[].class);
		moreVis.addFocusGroup("StreamGroup");
		
		display.showIODisplayTab();
		display.showItemControlTabInTree();
		display.setSize(800, 600);

		//passing the stream graph data to the prefuse data
		for (Layer layer : ls) {
			VisualItem vi = mark.drawCurve(layer.size, IntColor.GREEN, 1);
			vi.set("size", layer.size);
			moreVis.getFocusGroup("StreamGroup").addTuple(vi);
		}

		//processing all the data which are added to this group
		StreamGraphLayout layout = new StreamGraphLayout(
				"StreamGroup", PolygonRenderer.POLYGON, "size", 0);
		mark.addRenderer("StreamGroup", new StreamRenderer());
		//setting the color scheme for all the layers
		//if it is null, and the colors do not change
		layout.setColorPicker(new LinearColorPicker());
		//control for changing the orientation
		display.addWidget(BaseUIDisplay.IO_DISPLAY_TAB, layout.oriComboBox());
		
		//action for running the layout and repainting the display
		ActionList action = new ActionList();
		action.add(layout);
		action.add(new RepaintAction());
		moreVis.putAction("layout", action);
		
		//run the layout for the first time
		moreVis.run("layout");
		
		//resizing window will invoke the action to re-layout
		display.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				((BaseUIDisplay)e.getSource()).getVisualization().run("layout");
			}
		});
		//outline the hovered stripe
		display.addControlListener(new ControlAdapter(){
			@Override
			public void itemEntered(VisualItem item, MouseEvent e) {
				item.setStrokeColor(IntColor.YELLOW);
				item.getVisualization().repaint();
			}
			@Override
			public void itemExited(VisualItem item, MouseEvent e) {
				item.setStrokeColor(item.getFillColor());
				item.getVisualization().repaint();
			}
		});
		JFrame frame = display.frameView("Stream Graph Demo");
		frame.pack();
		frame.setVisible(true);
	}
}